package com.googlecode.gaal.suffix.impl;

import com.googlecode.gaal.data.api.IntSequence;
import com.googlecode.gaal.suffix.algorithm.api.ChildTableBuilder;
import com.googlecode.gaal.suffix.algorithm.api.LcpTableBuilder;
import com.googlecode.gaal.suffix.algorithm.api.SuffixTableBuilder;
import com.googlecode.gaal.suffix.api.LinearizedSuffixTree;
import com.googlecode.gaal.suffix.api.LinearizedSuffixTree.BinaryInterval;

public class LinearizedSuffixTreeBase extends AbstractBinaryIntervalTree<BinaryInterval> implements LinearizedSuffixTree {

    protected final BinaryInterval top;

    public LinearizedSuffixTreeBase(IntSequence sequence, int alphabetSize, SuffixTableBuilder suffixTableBuilder,
            LcpTableBuilder lcpTableBuilder, ChildTableBuilder childTableBuilder) {
        super(sequence, alphabetSize, suffixTableBuilder, lcpTableBuilder, childTableBuilder);
        top = new BinaryIntervalBase(0, suffixTable.length - 1);
    }

    @Override
    public BinaryInterval top() {
        return top;
    }

    protected class BinaryIntervalBase extends AbstractBinaryInterval implements BinaryInterval {

        protected BinaryIntervalBase(int left, int right) {
            super(left, right);
        }

        @Override
        public BinaryInterval leftChild() {
            return new BinaryIntervalBase(left, middle(left, right) - 1);
        }

        @Override
        public BinaryInterval rightChild() {
            return new BinaryIntervalBase(middle(left, right), right);
        }
    }
}
